﻿using CommunicaionLib.Enums;
using PLCLib;
using PLCLib.Model;
using PLC通讯框架开发.Model;
using PLC通讯框架开发.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PLC通讯框架开发
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private MainViewModel MainViewModel;
        private OrmonPlc OrmonPlc;
        public MainWindow()
        {
            InitializeComponent();
            
            MainViewModel = DataContext as MainViewModel;
            OrmonPlc = MainViewModel.OrmonPlc;
        }

        

        private void Button_Connect(object sender, RoutedEventArgs e)
        {
            if(OrmonPlc.ConnectedState == PLCLib.Enums.ConnectedState.通讯正常)
            {
                MainViewModel.ReconnectTip(this, new PlcEventArgs { Message= "设备已经连接无需再连接" });
            }
            else
            {
                Task.Run(() =>
                {
                    Net net = new Net();
                    net.LocalPort = 502;
                    net.LocalIP = "127.0.0.1";

                    net.IP = "127.0.0.1";
                    net.Port = 502;
                    OrmonPlc.Initialize(net);//初始化PLC
                    OrmonPlc.Start();

                    //OrmonPlc.Start(SetPraram);

                });
            }
        }

        /// <summary>
        /// 开始PLC的业务之前，先下发一些参数
        /// </summary>
        private void SetPraram()
        {
            Command command = new Command(OperatorMode.Write,AreaType.DM,DataType.WordHEX,20,100);
            OrmonPlc.Write(command,789456); 
        }

        

        private void Button_Read(object sender, RoutedEventArgs e)
        {
            Command command = new Command(OperatorMode.Read, AreaType.DM, DataType.WordHEX, 20, 100);
            var result = OrmonPlc.Read(command);
            if (result.Success)
            {
                //to do 解析 result.ReceiveBuffer
                MessageBox.Show("同步读取成功");
            }
        }

        private void Button_ReadAsync(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Command command = new Command(OperatorMode.Read, AreaType.DM, DataType.WordHEX, 2, 100);
                var result = OrmonPlc.ReadAsync(command);
                if (result.Result.Success)
                {
                    //to do 解析 result.ReceiveBuffer
                    MessageBox.Show("异步读取成功");
                }
            });
        }

        private void Button_Write(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Command command = new Command(OperatorMode.Write, AreaType.DM, DataType.WordHEX, 1, 100);
                var result = OrmonPlc.Write(command, 101);
                if (result.Success)
                {
                    //to do 解析 result.ReceiveBuffer
                    MessageBox.Show("同步写入数据成功");
                }
            });
        }

        private void Button_WriteAsync(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Command command = new Command(OperatorMode.Write, AreaType.DM, DataType.WordHEX, 1, 100);
                var result = OrmonPlc.WriteAsync(command, 101);
                if (result.Result.Success)
                {
                    //to do 解析 result.ReceiveBuffer
                    MessageBox.Show("异步写入数据成功");
                }
            });
        }

        private void Button_SendByte(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                byte[] array = new byte[] { 0x46,0x65 };
                var result = OrmonPlc.Send(array);
                if (result.Success)
                {
                    //to do 解析 result.ReceiveBuffer
                    MessageBox.Show("同步发送Byte成功");
                }
            });
        }

        private void Button_SendByteAsync(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                byte[] array = new byte[] { 0x46, 0x65 };
                var result = OrmonPlc.SendAsync(array);
                if (result.Result.Success)
                {
                    //to do 解析 result.ReceiveBuffer
                    MessageBox.Show("异步发送Byte成功");
                }
            });
        }

        private void Button_SendByteArray(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Command command = new Command(OperatorMode.Write, AreaType.DM, DataType.WordHEX, 10, 100);
                List<int> list = new List<int>() { 1,2,3,4,5,6,7,8,9,10};
                var result = OrmonPlc.Write(command, list);
                if (result.Success)
                {
                    //to do 解析 result.ReceiveBuffer
                    MessageBox.Show("同步写入int数组成功");
                }
            });
        }

        private void Button_SendByteArrayAsync(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Command command = new Command(OperatorMode.Write, AreaType.DM, DataType.WordHEX, 10, 100);
                List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                var result = OrmonPlc.WriteAsync(command, list);
                if (result.Result.Success)
                {
                    //to do 解析 result.ReceiveBuffer
                    MessageBox.Show("异步写入int数组成功");
                }
            });
        }


        private void Button_ReadPLC(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Command command = new Command(OperatorMode.Read, AreaType.DM, DataType.WordHEX, 2, 100);
                var result = OrmonPlc.Read<int>(command);
                if (result.Success)
                {
                    //to do 解析 result.ReceiveBuffer
                    Message message = new Message();
                    message.Content = $"读取泛型成功，值:{result.Content}";
                    App.Current.Dispatcher.Invoke((() => MainViewModel.MessagesList.Insert(0, message)));
                    
                    //MessageBox.Show($"读取泛型成功，值:{result.Content}");
                }
            });
        }

        private void Button_WritePLC(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Command command = new Command(OperatorMode.Write, AreaType.DM, DataType.WordHEX, 10, 100);
                List<short> list = new List<short>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                var result = OrmonPlc.Write<short[]>(command, list.ToArray());
                if (result.Success)
                {
                    //to do 解析 result.ReceiveBuffer
                    MessageBox.Show("同步写入泛型成功");
                }
            });
        }
    }
}
